﻿using AutoMapper;
using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Service.AutoMapper {
    public class TokenBillMapper : IStartupDto {
        private IMapperConfigurationExpression _cfg;
        public TokenBillMapper(IMapperConfigurationExpression cfg) {
            _cfg = cfg;
        }
        public void Execute() {
            Create();
        }
        protected virtual void Create() {
            _cfg.CreateMap<token_bill_detial_dto, TokenBillDetail>()
             .ForMember(dest => dest.Id, src => src.MapFrom(c => c.id))
             .ForMember(dest => dest.TokenBillId, src => src.MapFrom(c => c.token_bill_id))
             .ForMember(dest => dest.MerchantId, src => src.MapFrom(c => c.merchant_id))
             .ForMember(dest => dest.InviteMerchantId, src => src.MapFrom(c => c.invite_merchantId))
             .ForMember(dest => dest.UserType, src => src.MapFrom(c => c.user_type))
             .ForMember(dest => dest.Commission, src => src.MapFrom(c => c.commission))
             .ForMember(dest => dest.Proportion, src => src.MapFrom(c => c.proportion))
             .ForMember(dest => dest.IsComfirm, src => src.MapFrom(c => c.is_comfirm))
             .ForMember(dest => dest.IsBus, src => src.MapFrom(c => c.is_bus))
             .ForMember(dest => dest.PayId, src => src.MapFrom(c => c.pay_id))
             .ForMember(dest => dest.TransferVoucher, src => src.MapFrom(c => c.transfer_voucher))
             .ForMember(dest => dest.ComfirmTransferVoucher, src => src.MapFrom(c => c.comfirm_transferVoucher))
             .ForMember(dest => dest.ReceivablesInfo, src => src.MapFrom(c => c.recivables_info.ToJson()))
             .ForMember(dest => dest.UpdateTime, src => src.MapFrom(c => c.update_time.ToUnix()))
             .ForMember(dest => dest.CreateTime, src => src.MapFrom(c => c.create_time.ToUnix()));

            _cfg.CreateMap<TokenBillDetail, token_bill_detial_dto>()
           .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
           .ForMember(dest => dest.token_bill_id, src => src.MapFrom(c => c.TokenBillId))
           .ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.MerchantId))
           .ForMember(dest => dest.merchant_name, src => src.MapFrom(c => c.Merchant.Mobile))
           .ForMember(dest => dest.merchant_user_type, src => src.MapFrom(c => c.Merchant.UserType))
           .ForMember(dest => dest.merchant_user_type_desc, src => src.MapFrom(c => c.Merchant.UserType.ToDescription()))
           .ForMember(dest => dest.invite_merchantId, src => src.MapFrom(c => c.InviteMerchantId))
           .ForMember(dest => dest.invite_merchant_name, src => src.MapFrom(c => c.InviteMerchant.Mobile))
           .ForMember(dest => dest.apply_merchantId, src => src.MapFrom(c => c.TokenBill.ApplyMerchantId))
           .ForMember(dest => dest.apply_merchant_name, src => src.MapFrom(c => c.TokenBill.ApplyMerchant.Mobile))
           .ForMember(dest => dest.user_type, src => src.MapFrom(c => c.UserType))
           .ForMember(dest => dest.is_bus, src => src.MapFrom(c => c.IsBus))
           .ForMember(dest => dest.pay_id, src => src.MapFrom(c => c.PayId))
           .ForMember(dest => dest.user_type_desc, src => src.MapFrom(c => "升级" + c.UserType.ToDescription()))
           .ForMember(dest => dest.commission, src => src.MapFrom(c => c.Commission))
           .ForMember(dest => dest.proportion, src => src.MapFrom(c => c.Proportion))
           .ForMember(dest => dest.is_comfirm, src => src.MapFrom(c => c.IsComfirm))
           .ForMember(dest => dest.transfer_voucher, src => src.MapFrom(c => c.TransferVoucher))
           .ForMember(dest => dest.comfirm_transferVoucher, src => src.MapFrom(c => c.ComfirmTransferVoucher))
           .ForMember(dest => dest.recivables_info, src => src.MapFrom(c => c.ReceivablesInfo.ToObject<recivables_dto>()))
           .ForMember(dest => dest.update_time, src => src.MapFrom(c => c.UpdateTime.ToDateTime()))
           .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.CreateTime.ToDateTime()));
            //钱包课程收益
            _cfg.CreateMap<TokenBillDetail, token_wallet_bill_detail_dto>()
             .ForMember(dest => dest.nick_name, src => src.MapFrom(c => c.TokenBill.ApplyMerchant.Name))
             .ForMember(dest => dest.mobile, src => src.MapFrom(c => c.TokenBill.ApplyMerchant.Mobile))
             .ForMember(dest => dest.money, src => src.MapFrom(c => c.Commission.ToString("f2")))
             .ForMember(dest => dest.datetime, src => src.MapFrom(c => c.CreateTime.ToDateTime().ToString()))
             .ForMember(dest => dest.order_no, src => src.Ignore())
             .ForMember(dest => dest.status, src => src.Ignore())
             .ForMember(dest => dest.status_desc, src => src.Ignore());
            _cfg.CreateMap<Profit, token_wallet_bill_detail_dto>()
                .AfterMap((src, dest) => {
                    string result = "";
                    if (src.Product != null) {
                        result = src.Product.Name.Length > 15 ? src.Product.Name.Substring(0, 15) + "..." : src.Product.Name;
                    }
                    dest.order_no = (string.IsNullOrEmpty(result) ? src.CreateTime.ToDateTime().ToString("yyyy-MM-dd HH:mm:ss") : result);
                })
                .ForMember(dest => dest.order_id, src => src.MapFrom(c => c.OrderId))
                .ForMember(dest => dest.status, src => src.MapFrom(c => c.BillStatus))
                .ForMember(dest => dest.datetime, src => src.MapFrom(c => c.CreateTime.ToDateTime().ToString("yyyy-MM-dd HH:mm:ss")))
                .ForMember(dest => dest.status_desc, src => src.MapFrom(c => c.BillStatus.ToDescription()))
                .ForMember(dest => dest.money, src => src.MapFrom(c => c.Amount.ToString("f2")));
            _cfg.CreateMap<TokenBill, token_bill_dto>()
                .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
                .ForMember(dest => dest.apply_merchant_id, src => src.MapFrom(c => c.ApplyMerchantId))
                .ForMember(dest => dest.apply_merchant_name, src => src.MapFrom(c => c.ApplyMerchant.Mobile))
                .ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.MerchantId))
                .ForMember(dest => dest.merchant_name, src => src.MapFrom(c => c.Merchant.Mobile))
               .ForMember(dest => dest.city_company_merchant_id, src => src.MapFrom(c => c.CityCompanyMerchantId))
                .ForMember(dest => dest.city_company_mobile, src => src.MapFrom(c => c.CityCompanyMerchant.Mobile))
                .ForMember(dest => dest.total_commission, src => src.MapFrom(c => c.TotalCommission))
                .ForMember(dest => dest.user_type, src => src.MapFrom(c => c.UserType))
                .ForMember(dest => dest.user_type_desc, src => src.MapFrom(c => "升级" + c.UserType.ToDescription()))
                .ForMember(dest => dest.e_maid_status, src => src.MapFrom(c => c.EMaidStatus))
                .ForMember(dest => dest.e_maid_status_desc, src => src.MapFrom(c => c.EMaidStatus.ToDescription()))
                .ForMember(dest => dest.invite_merchant_path, src => src.MapFrom(c => c.InviteMerchantPath))
                .ForMember(dest => dest.profit, src => src.MapFrom(c => c.Profit))
                .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.CreateTime.ToDateTime()))
                 .ForMember(dest => dest.launch_time, src => src.MapFrom(c => c.LaunchTime.ToDateTime()));

            _cfg.CreateMap<token_bill_dto, TokenBill>()
               .ForMember(dest => dest.Id, src => src.MapFrom(c => c.id))
               .ForMember(dest => dest.ApplyMerchantId, src => src.MapFrom(c => c.apply_merchant_id))
               .ForMember(dest => dest.MerchantId, src => src.MapFrom(c => c.merchant_id))
               .ForMember(dest => dest.CityCompanyMerchantId, src => src.MapFrom(c => c.city_company_merchant_id))
               .ForMember(dest => dest.TotalCommission, src => src.MapFrom(c => c.total_commission))
               .ForMember(dest => dest.UserType, src => src.MapFrom(c => c.user_type))
               .ForMember(dest => dest.EMaidStatus, src => src.MapFrom(c => c.e_maid_status))
               .ForMember(dest => dest.InviteMerchantPath, src => src.MapFrom(c => c.invite_merchant_path))
               .ForMember(dest => dest.Profit, src => src.MapFrom(c => c.profit))
               .ForMember(dest => dest.CreateTime, src => src.MapFrom(c => c.create_time.ToUnix()))
               .ForMember(dest => dest.LaunchTime, src => src.MapFrom(c => c.launch_time.ToUnix()));


            #region 供应商PC
            _cfg.CreateMap<Profit, spc_token_bill_detail_dto>()
                .ForMember(dest => dest.datetime, src => src.MapFrom(c => c.CreateTime.ToDateTime().ToString("yyyy-MM-dd HH:mm:ss")))
                .ForMember(dest => dest.order_id, src => src.MapFrom(c => c.OrderId))
                .ForMember(dest => dest.order_no, src => src.MapFrom(c => c.Order != null ? c.Order.OrderNo : ""))
                .ForMember(dest => dest.pay_no, src => src.MapFrom(c => c.Order != null && c.Order.PayInfo != null ? c.Order.PayInfo.PayNo : ""))
                .ForMember(dest => dest.name, src => src.MapFrom(c => c.Order != null && c.Order.Buyer != null ? c.Order.Buyer.Name : "匿名"))
                .ForMember(dest => dest.money, src => src.MapFrom(c => c.Amount.ToString("f2")))
                .ForMember(dest => dest.status, src => src.MapFrom(c => c.BillStatus))
                .ForMember(dest => dest.status_desc, src => src.MapFrom(c => c.BillStatus.ToDescription()));
            #endregion


        }
    }
}
